package cn.linlane.config;

import cn.linlane.entity.sys.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

/**
 * @author cheng
 * @date 2018-06-08 15:46
 */
@Configuration
public class SpringSecurityAuditorAware implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {

        Subject subject = SecurityUtils.getSubject();

        if (subject == null || !subject.isAuthenticated()) {
            return Optional.empty();
        }

        SysUser sysUser = (SysUser) subject.getPrincipals().fromRealm("myRealm").iterator().next();

        if (sysUser != null){
            return Optional.of(sysUser.getAccount());
        }else {
            return Optional.empty();
        }

    }

}
